package com.csust.base;

import java.sql.*;

//第一个jdbc程序
public class jdbcFirst {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //1.加载驱动
//        Class.forName("com.mysql.jdbc.Driver");//加载驱动 固定写法
//        DriverManager.registerDriver(new com.mysql.jdbc.Driver());

        //2.用户信息与url
        String url = "jdbc:mysql://localhost:3306/atguigu?useUnicode=true&characterEncoding=utf8&useSSL=false";
        String username = "root";
        String password = "123456";

        //3.连接成功,数据库对象
        Connection connection = DriverManager.getConnection(url, username, password);
        //4.执行sql的对象
        Statement statement = connection.createStatement();
        //5.执行sql的对象 去执行sql
        String sql = "select * from `t_emp`";
        ResultSet resultSet = statement.executeQuery(sql);
        while (resultSet.next()) {
            int empId = resultSet.getInt("emp_id");
            String empName = resultSet.getString("emp_name");
            Double empSalary = resultSet.getDouble("emp_salary");
            int empAge = resultSet.getInt("emp_age");
            System.out.println(empId + "\t" + empName + "\t" + empSalary + "\t" + empAge);

        }
        //6.释放连接
        resultSet.close();
        statement.close();
        connection.close();
    }

}
